﻿/*  
	========================================================================
	 awzBoxes Library
	
	 Homepage: http://www.awzhome.de/
	========================================================================
	
	This Source Code Form is subject to the terms of the Mozilla Public
	License, v. 2.0. If a copy of the MPL was not distributed with this
	file, You can obtain one at http://mozilla.org/MPL/2.0/.
   
	The Original Code is code of awzBoxes Library.

	The Initial Developer of the Original Code is Andreas Weizel.
	Portions created by the Initial Developer are
	Copyright (C) 2011-2012 Andreas Weizel. All Rights Reserved.
	
	Contributor(s): (none)
	
	========================================================================
*/

using System;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace AWZhome.awzBoxes
{
	/// <summary>
	/// Represents a <see cref="AWZhome.awzBoxes.Box"/> that is in auto-hidden mode.
	/// </summary>
	public class AutoHiddenBox
	{
		/// <summary>
		/// Constructor of AutoHiddenBox class.
		/// </summary>
		/// <param name="parentBoxArea">Instance of the parent <see cref="AWZhome.awzBoxes.BoxArea"/>.</param>
		public AutoHiddenBox(BoxArea parentBoxArea)
		{
			this.Items = new ObservableCollection<BoxItem>();
			this.Items.CollectionChanged += BoxItems_CollectionChanged;
			this.ParentBoxArea = parentBoxArea;
		}

		/// <summary>
		/// Returns the collection of <see cref="AWZhome.awzBoxes.BoxItem"/> elements in
		/// the auto-hidden Box.
		/// </summary>
		public ObservableCollection<BoxItem> Items
		{
			get;
			private set;
		}

		/// <summary>
		/// Returns the parent <see cref="AWZhome.awzBoxes.BoxArea"/>, which contains
		/// this AutoHiddenBox.
		/// </summary>
		public BoxArea ParentBoxArea
		{
			get;
			private set;
		}

		private void BoxItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			switch (e.Action)
			{
				case NotifyCollectionChangedAction.Add:
					if (e.NewItems != null)
					{
						foreach (var item in e.NewItems)
						{
							BoxItem boxItem = item as BoxItem;
							if (boxItem != null)
							{
								boxItem.ParentAutoHiddenBox = this;
								if (this.ParentBoxArea != null)
								{
									boxItem._Direction = this.ParentBoxArea.Direction;
								}
							}
						}
					}
					break;

				case NotifyCollectionChangedAction.Move:
					break;

				case NotifyCollectionChangedAction.Remove:
					if (e.OldItems != null)
					{
						foreach (var item in e.OldItems)
						{
							BoxItem boxItem = item as BoxItem;
							if (boxItem != null)
							{
								boxItem.ParentAutoHiddenBox = null;
								if (boxItem.ParentBox == null)
								{
									boxItem._Direction = BoxItemDockDirection.NotDocking;
								}
							}
						}
					}
					break;

				case NotifyCollectionChangedAction.Replace:
					if (e.OldItems != null)
					{
						foreach (var item in e.OldItems)
						{
							BoxItem boxItem = item as BoxItem;
							if (boxItem != null)
							{
								boxItem.ParentAutoHiddenBox = null;
								if (boxItem.ParentBox == null)
								{
									boxItem._Direction = BoxItemDockDirection.NotDocking;
								}
							}
						}
					}
					if (e.NewItems != null)
					{
						foreach (var item in e.NewItems)
						{
							BoxItem boxItem = item as BoxItem;
							if (boxItem != null)
							{
								boxItem.ParentAutoHiddenBox = this;
							}
						}
					}
					break;

				case NotifyCollectionChangedAction.Reset:
					break;

				default:
					break;
			}

			// Remove this AutoHiddenBox from parent, if we have not items any more
			if (this.Items.Count == 0)
			{
				if (this.ParentBoxArea != null)
				{
					this.ParentBoxArea.RestoreAutoHiddenBox(this);
				}
			}
		}
	}
}
